package com.neo.service;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.neo.util.DjsStringUtil;


@Service
public class DjsSignFileService {

	@Autowired
    private JdbcTemplate jdbcTemplate;
		String errcode = null;
		public String readResFile(List<String> list) throws Exception{
		int res_f = 0;
		int res_s = 0;
		int res_n = 0;
		for (String str : list) {
			String flag = DjsStringUtil.cutByGBK(str,39,1);
			switch (flag) {
				case "F":
					errcode = DjsStringUtil.cutByGBK(str,40,3);//pass
					System.out.println("失败原因："+errcode);
					res_f++;
					break;
				case "S":
					String cardnbr = DjsStringUtil.cutByGBK(str,0,19);//todo
					String idno = DjsStringUtil.cutByGBK(str,19,18);
					updateUserCardnbr(idno,cardnbr);
					res_s++;
					break;
				case "N":
					errcode = DjsStringUtil.cutByGBK(str,40,3);//pass
					System.out.println("失败原因："+errcode);
					res_n++;
					break;
				default:
					break;
			}
		}

		System.out.println("失败F条数"+res_f);
		System.out.println("成功S条数"+res_s);
		System.out.println("失败N条数"+res_n);
		return "success";
	}
	private void updateUserCardnbr(String idno, String cardnbr) {
		String sql = "UPDATE inv_user  SET  cardnbr  =?  WHERE IDENTITY_NO=? ";
		int i = jdbcTemplate.update(sql,cardnbr,idno);
	}
	public String generalFile(String batch) throws Exception{
		List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT ID,ACCOUNT_ID FROM jx_account_info  ", new Object[]{});
		List<String> resList = new ArrayList<>();
		for (Map map : list) {
			String account_id = (String)map.get("ACCOUNT_ID");
			Long id = (Long)map.get("ID");

			SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMddHHmmss");
			Date date=new Date();
			String strDate =dateFormater.format(date);

			String seri_no = DjsStringUtil.COINSTCODE+"0000"+id.toString();
			
			String res = DjsStringUtil.BANKID
					+ batch 
					+ DjsStringUtil.fillBlank(account_id, 19) //签约电子账号
					+ DjsStringUtil.fillBlank(DjsStringUtil.FUISSUER, 4) //产品发行方
					+ "2" //签约类型  2:自动债权转让签约
					+ DjsStringUtil.fillRightZero(seri_no, 40)// 签约流水号
					+ strDate // 签约日期 签约时间
					+ DjsStringUtil.fillBlank(DjsStringUtil.BLANK, 100) 	//保留域
					+ DjsStringUtil.fillBlank(DjsStringUtil.BLANK, 100)		//第三方保留域
					;
			System.out.println(res);

	        byte[] bs = res.getBytes("GBK");
	        System.out.println(bs.length);
			if (bs.length != 288) {
				System.out.println("生成出错");
			};
			resList.add(res);
		}
		System.out.println("生成文件行数"+resList.size());
		
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMdd");
		Date date=new Date();
		String strDate =dateFormater.format(date);
		
		String fileName = DjsStringUtil.FILE_PATH+DjsStringUtil.BANKID+"-"+"XX"+"-SIGTRAN-"+batch+"-"+strDate;
		DjsStringUtil.writeStringToFile(resList,batch,fileName);
		
		return fileName;
	}

	
	private String getEmail(String email) {
		if (email == null) {
			return DjsStringUtil.BLANK;
		}
		if (email.length()>40) {
			email = email.substring(0,40);
		}
		return email;
	}
	private long getAppid() {
		java.util.Random r=new java.util.Random(); 
		return System.currentTimeMillis()+r.nextInt(99999);
	}
	
	private boolean checkPhone(String name) throws ParseException {
		if (name==null || name.length()>60 || name.length()==0) {
			return false;
		}
		return true;
	}
	private boolean checkName(String name) throws ParseException {
		if (name==null || name.length()>60 || name.length()==0) {
			return false;
		}
		return true;
	}
	private boolean checkIDNO(String idno) throws ParseException {
		if (idno==null || idno.length()!=18) {
			return false;
		}
		int year = Integer.parseInt(idno.substring(6, 10));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date update = sdf.parse(String.valueOf(year + 18) + idno.substring(10, 14));
        Date today = new Date();
        if (!today.after(update)) {
            System.out.println("error id"+idno);
        	return false;
        }
        
		return true;
	}
	
	public static String formatString(String in,int len) {
		String s="123中国123";
		return String.format("%1$-"+len+"s", in);
	}
	public static void main(String[] args) throws UnsupportedEncodingException {
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date=new Date();
		String strDate =dateFormater.format(date);
		System.out.println(strDate);
	}
}